//
//  NSString+Path.m
//  DoctorYL
//
//  Created by 陈腾飞 on 16/1/10.
//  Copyright © 2016年 yuntai. All rights reserved.
//

#import "NSString+Path.h"
#import "IWxChatMgrChatDefs.h"
#define DATABASE_PATH  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]


@implementation NSString (Path)

+ (void)removeAllFileWithFolder:(NSString *)folderStr {
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:folderStr];
    for (NSString *p in files) {
        NSError *error;
        NSString *Path = [folderStr stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
}

+ (CGFloat)folderSizeAtPath:(NSString *)folderPath {
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

+ (long long)fileSizeAtPath:(NSString *)filePath {
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

/**
 *  录制视频时使用到的路径
 *
 *  @return
 */
+ (NSString *)videoRecordPath
{
    NSString *fileName = [NSProcessInfo processInfo].globallyUniqueString;
    NSString *localFilePathName = [NSString wbChatPathWithType:kChatMsgBody_Video];
    [NSString checkFileFolderPathExist:localFilePathName];
    NSString *path = [localFilePathName stringByAppendingPathComponent:\
                      [fileName stringByAppendingPathExtension:@"mp4"]];
    return path;
}
/**
 *  创建聊天相关的文件存储路径
 *
 *  @param messageType 文件类型 －image/vioce/video--区分为图片、语音、小视频
 *
 *  @return
 */
+ (NSString *)wbChatPathWithType:(NSString *)messageType
{
    NSString *localFilePathName = [NSString stringWithFormat:@"%@/%@/%@",DATABASE_PATH,\
                                   kUserLoginName,messageType];
    return localFilePathName;
    
}

//检查用户Id文件目录下的image文件夹是否存在，若不存在则创建
+ (void)checkFileFolderPathExist:(NSString *)fileFolderPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:fileFolderPath]) {
        [fileManager createDirectoryAtPath:fileFolderPath
               withIntermediateDirectories:YES
                                attributes:nil error:nil];
    }
}

@end
